﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic;
using BusinessObjects;

namespace Presentation {
  /// <summary>
  /// Postcode form
  /// </summary>
  public partial class NewZipcodeForm : Form {

    private ZipcodeBll bll = new ZipcodeBll( );
    private List<Country> countryList = new List<Country>( );
    private Country country = new Country( );
    private CityZipcode zipcode;

    #region - Constructor -

    /// <summary>
    /// Initializes a new instance of the <see cref="NewZipcodeForm"/> class.
    /// </summary>
    public NewZipcodeForm( ) {
      InitializeComponent( );
    }

    #endregion

    #region - Events -

    private void NewZipcodeForm_Load( object sender, EventArgs e ) {
      countryList = bll.GetCountries( );
      countryBindingSource.DataSource = countryList;
      zipcode = new CityZipcode( ( Country ) countriesComboBox.SelectedItem );
      zipcode.MarkClean( );
    }

    private void clearButton_Click( object sender, EventArgs e ) {
      ClearZipcode( );
    }

    private void saveButton_Click( object sender, EventArgs e ) {
      if ( ValidateForm( ) ) {
        SetZipcode( );

        bll.SaveZipcode( zipcode );

        ClearZipcode( );
      }
    }

    private void editButton_Click( object sender, EventArgs e ) {
      ZipcodeListForm zipcodeForm = new ZipcodeListForm( );
      if ( zipcodeForm.ShowDialog( ) == DialogResult.OK ) {
        zipcode = zipcodeForm.Zipcode;
        this.countriesComboBox.SelectedValue = zipcodeForm.Zipcode.CountryId;
        this.zipcodeTextBox.Text = zipcodeForm.Zipcode.Zipcode;
        this.cityTextBox.Text = zipcodeForm.Zipcode.City;

      }
    }

    private void deleteButton_Click( object sender, EventArgs e ) {
      if ( MessageBox.Show( "Deze postcode verwijderen?", "Postcode verwijderen", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == System.Windows.Forms.DialogResult.Yes ) {
        bll.DeleteZipcode( zipcode );
        ClearZipcode( );
      }
    }

    private void NewZipcodeForm_FormClosing( object sender, FormClosingEventArgs e ) {
      SetZipcode( );
      if ( zipcode.HasChanged ) {
        if ( MessageBox.Show( "Postcode is gewijzigd. Bewaren?", "Postcode gewijzigd", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == System.Windows.Forms.DialogResult.Yes ) {
          if ( ValidateForm( ) ) {
            bll.SaveZipcode( zipcode );
          } else {
            e.Cancel = true;
          }
        }
      }
    }

    #endregion

    #region - Methods -

    private bool ValidateForm( ) {
      bool valid = true;
      errorProvider.Clear( );

      if ( string.IsNullOrEmpty( zipcodeTextBox.Text.Trim( ) ) ) {
        errorProvider.SetError( zipcodeTextBox, "Postcode verplicht." );
        errorProvider.SetIconAlignment( zipcodeTextBox, ErrorIconAlignment.MiddleLeft );
        valid = false;
      }

      if ( string.IsNullOrEmpty( cityTextBox.Text.Trim( ) ) ) {
        errorProvider.SetError( cityTextBox, "Gemeente verplicht." );
        errorProvider.SetIconAlignment( cityTextBox, ErrorIconAlignment.MiddleLeft );
        valid = false;
      }

      return valid;
    }

    private void SetZipcode( ) {
      zipcode.Zipcode = zipcodeTextBox.Text;
      zipcode.City = cityTextBox.Text;
      zipcode.CountryName = ( ( Country ) countriesComboBox.SelectedItem ).CountryName;
      zipcode.CountryCode = ( ( Country ) countriesComboBox.SelectedItem ).CountryCode;
      zipcode.CountryId = ( ( Country ) countriesComboBox.SelectedItem ).CountryId;
    }

    private void ClearZipcode( ) {
      countriesComboBox.SelectedIndex = 0;
      zipcodeTextBox.Clear( );
      cityTextBox.Clear( );
      zipcode = new CityZipcode( ( Country ) countriesComboBox.SelectedItem );
      zipcode.MarkClean( );
    }

    #endregion

  }
}
